<?php

class Mod_authentication extends CI_Model {
    
    /* ------------------------------------------------------------------
     * Check username and password. If exist, create three sessions. 
	 * ------------------------------------------------------------------
     * 1. admin: user logged in
     * 2. userId: user's Id
     * 3. userName: user's name
     * 3. userPhoto: user's photo
     * @param string $username
     * @param string $password
     * @return bool 
     */
	 
    public function login($username, $password){
        $this->db->select('*');
        $this->db->where(field('username'), $username);
        $this->db->where(field('password'), $password);
        $this->db->where(field('roleId'), 2);
        $this->db->where(field('isDisable'), 0);
        $this->db->where(field('isVerify'), 1);
        $this->db->where(field('deletedAt'), NULL);
        $this->db->from(table('user'));
        $data=$this->db->get();
        if($data->num_rows()>0){
            $userData=$data->row_array();
            $this->session->set_userdata('isSiteLogin', TRUE);
            $this->session->set_userdata('userId', $userData[field('id')]);
            $this->session->set_userdata('userName', $userData[field('lname')].' '.$userData[field('fname')]);
            $this->session->set_userdata('userRole', $userData[field('roleId')]);
            $this->session->set_userdata('userPhoto', $userData[field('image')]);
            return TRUE;
        }else{
            return FALSE;
        }
    }
}
